<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"
    import="login.LoginMachine, java.util.ArrayList;"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Account Management</title>
</head>
<body>

<%
try {
	String lgnStatus = request.getParameter("status"); 
	if (lgnStatus.equals("failed")) {
		out.println("<div style='color:red'><p>An error occured when trying to create or update the account.</div>Please try again or create an account.</p>");
	} 
}
catch(NullPointerException e) {
	//do nothing 
}
%>

<%
//checks for username cookie. 
	
	String action = "Submit"; 
	Cookie[] cookies = request.getCookies();
	Cookie c = null; 
	ArrayList<String> userData = new ArrayList<String>(); 
	boolean cookieFound = false; 
	
	if (cookies.length > 0) {	
		for (int i = 0; i < cookies.length; i++) {
			c = cookies[i]; 
			String name = c.getName(); 
			if (name.equals("username")){
				cookieFound = true; 
				break; 
			}
		}
	}
	
	if (cookieFound == true) {
		LoginMachine lm = new LoginMachine(); 
		userData = lm.getUserData(c.getValue());
		action = "Update"; 
		out.println("<h3>Update your account information:</h3>"); 
	}
	else {
		for(int j=0; j<10; j++) {
			userData.add(""); 
			out.println("<h3>Register a new account:</h3>"); 
		}
	}

	
%> 


<form method=post action="NewAccount">
<p>User name: <input type="text" name=username size=10 value='<%= userData.get(0) %>'></p>
<p>Password: <input type="text" name=password size=10 value='<%= userData.get(2) %>'> </p>
<p>Email Address: <input type="text" name=email size=10 value='<%= userData.get(1) %>'> </p>
<p>First Name: <input type="text" name=firstname size=10 value='<%= userData.get(3) %>' > </p>
<p>Last Name: <input type="text" name=lastname size=10 value='<%= userData.get(4) %>'> </p>
<p>Phone Number: <input type="text" name=contactnum size=10 value='<%= userData.get(5)%>'> </p>
<input type="hidden" name="action" value='<%= action %>'> 
<p><input type=SUBMIT value='<%= action %>'></p>
</form>

</body>
</html>